﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using WebReference;
using Session;

public partial class Admin_Struct : System.Web.UI.Page
{
    Service objService = new Service();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack)
        {
        }
        else
        {
            string ExperimentId = (string)Session["ExperimentId"];

            if (ExperimentId != null)
            {
                lblExpdetails.Text = "Your Experiment Id is - " + ExperimentId;
            }
            string id = Request.QueryString["id"];

            if (id != null)
            {
                fillStructuralDetails(Convert.ToInt32(id));
                txtStructId.Text = string.Empty;
                trDgStruct.Visible = false;
                trStruct.Visible = true;
            }

            bindDataGrid();
        }
    }
    private void bindDataGrid()
    {
        string ExpId = (string)Session["ExperimentId"];
        if (ExpId != null)
        {
            Int32 StructId = objService.GetExperimentFKValues("struct_fk", ExpId);
            if (StructId.ToString() == null)
            {
                lblMessage.Text = "You haven't entered any data for the Structural analysis to this experiment";
                trDgStruct.Visible = false;
                trStruct.Visible = true;
            }
            else
            {
                if (objService.GetStruct(StructId.ToString()).Tables[0].Rows.Count > 0)
                {
                    dgStruct.DataSource = objService.GetStruct(StructId.ToString());
                    dgStruct.DataBind();
                    trDgStruct.Visible = true;
                    trStruct.Visible = false;
                }
                else
                {
                    trDgStruct.Visible = false;
                    trStruct.Visible = true;
                }
            }
        }
        else
        {
            lblMessage.Text = "You haven't entered any data for the Structural analysis to this experiment";
            trDgStruct.Visible = false;
            trStruct.Visible = true;
        }
    }

    private void clearTextBoxes()
    {
        txtStructId.Text = string.Empty;
        txtLignin.Text = string.Empty;
        txtLigninMethod.Text = string.Empty;
        txtCellulose.Text = string.Empty;
        txtHemiCellulose.Text = string.Empty;
        txtExtractives.Text = string.Empty;
        txtCellWall.Text = string.Empty;
        txtPectin.Text = string.Empty;
        txtSolubleSugarStarch.Text = string.Empty;
        txtXylan.Text = string.Empty;
        txtGlucomanna.Text = string.Empty;
        txtgalactan.Text = string.Empty;
        txtarabinan.Text = string.Empty;
        txtmannan.Text = string.Empty;
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        
    }
    private void fillStructuralDetails(Int32 id)
    {
        DataSet ds = new DataSet();
        ds = objService.GetTableDetailsBasedonPK("struct", id);
        txtStructId.Text = ds.Tables[0].Rows[0]["id"].ToString();
        txtarabinan.Text = ds.Tables[0].Rows[0]["arabinan"].ToString();
        txtLignin.Text = ds.Tables[0].Rows[0]["lignin"].ToString();
        txtLigninMethod.Text = ds.Tables[0].Rows[0]["ligninMethod"].ToString();
        txtCellulose .Text = ds.Tables[0].Rows[0]["cellulose"].ToString();
        txtHemiCellulose.Text = ds.Tables[0].Rows[0]["hemicellulose"].ToString();
        txtExtractives.Text = ds.Tables[0].Rows[0]["extractives"].ToString();
        txtCellWall.Text = ds.Tables[0].Rows[0]["cellwall"].ToString();
        txtPectin.Text = ds.Tables[0].Rows[0]["pectin"].ToString();
        txtSolubleSugarStarch.Text = ds.Tables[0].Rows[0]["sol_sugars_stch"].ToString();
        txtXylan.Text = ds.Tables[0].Rows[0]["xylan"].ToString();
        txtGlucomanna.Text = ds.Tables[0].Rows[0]["glucomannan"].ToString();
        txtgalactan.Text = ds.Tables[0].Rows[0]["galactan"].ToString();
        txtmannan.Text = ds.Tables[0].Rows[0]["mannan"].ToString();
    }
    protected void dgStruct_RowEditing(object sender, GridViewEditEventArgs e)
    {
        int id = Convert.ToInt32(dgStruct.Rows[e.NewEditIndex].Cells[1].Text.Trim());
        fillStructuralDetails(id);
        trDgStruct.Visible = false;
        trStruct.Visible = true;
        btnPrevious.Visible = false;
    }
    protected void btnSave_Click1(object sender, ImageClickEventArgs e)
    {
        string ReferenceID = (string)Session["ReferenceId"];

        if (txtStructId.Text != string.Empty)
        {
            if (txtLignin.Text == string.Empty)
            {
                lblMessage.Text = objService.UpdateTable("struct", "lignin", "null", txtStructId.Text);
            }
            else
            {
                lblMessage.Text = objService.UpdateTable("struct", "lignin", txtLignin.Text, txtStructId.Text);
            }

            if (txtLigninMethod.Text == string.Empty)
            {
                lblMessage.Text = objService.UpdateTable("struct", "ligninMethod", "null", txtStructId.Text);
            }
            else
            {
                lblMessage.Text = objService.UpdateTable("struct", "ligninMethod", txtLigninMethod.Text, txtStructId.Text);
            }

            if (txtCellulose.Text == string.Empty)
            {
                lblMessage.Text = objService.UpdateTable("struct", "cellulose", "null", txtStructId.Text);
            }
            else
            {
                lblMessage.Text = objService.UpdateTable("struct", "cellulose", txtCellulose.Text, txtStructId.Text);
            }
            if (txtHemiCellulose.Text == string.Empty)
            {
                lblMessage.Text = objService.UpdateTable("struct", "hemicellulose", "null", txtStructId.Text);
            }
            else
            {
                lblMessage.Text = objService.UpdateTable("struct", "hemicellulose", txtHemiCellulose.Text, txtStructId.Text);
            }
            if (txtExtractives.Text == string.Empty)
            {
                lblMessage.Text = objService.UpdateTable("struct", "extractives", "null", txtStructId.Text);
            }
            else
            {
                lblMessage.Text = objService.UpdateTable("struct", "extractives", txtExtractives.Text, txtStructId.Text);
            }
            if (txtCellWall.Text == string.Empty)
            {
                lblMessage.Text = objService.UpdateTable("struct", "cellwall", "null", txtStructId.Text);
            }
            else
            {
                lblMessage.Text = objService.UpdateTable("struct", "cellwall", txtCellWall.Text, txtStructId.Text);
            }
            if (txtPectin.Text == string.Empty)
            {
                lblMessage.Text = objService.UpdateTable("struct", "pectin", "null", txtStructId.Text);
            }
            else
            {
                lblMessage.Text = objService.UpdateTable("struct", "pectin", txtPectin.Text, txtStructId.Text);
            }

            if (txtSolubleSugarStarch.Text == string.Empty)
            {
                lblMessage.Text = objService.UpdateTable("struct", "sol_sugars_stch", "null", txtStructId.Text);
            }
            else
            {
                lblMessage.Text = objService.UpdateTable("struct", "sol_sugars_stch", txtSolubleSugarStarch.Text, txtStructId.Text);
            }
            if (txtXylan.Text == string.Empty)
            {
                lblMessage.Text = objService.UpdateTable("struct", "xylan", "null", txtStructId.Text);
            }
            else
            {
                lblMessage.Text = objService.UpdateTable("struct", "xylan", txtXylan.Text, txtStructId.Text);
            }
            if (txtGlucomanna.Text == string.Empty)
            {
                lblMessage.Text = objService.UpdateTable("struct", "glucomannan","null", txtStructId.Text);
            }
            else
            {
                lblMessage.Text = objService.UpdateTable("struct", "glucomannan", txtGlucomanna.Text, txtStructId.Text);
            }
            if (txtgalactan.Text == string.Empty)
            {
                lblMessage.Text = objService.UpdateTable("struct", "galactan", "null", txtStructId.Text);
            }
            else
            {
                lblMessage.Text = objService.UpdateTable("struct", "galactan", txtgalactan.Text, txtStructId.Text);
            }

            if (txtarabinan.Text == string.Empty)
            {
                lblMessage.Text = objService.UpdateTable("struct", "arabinan", "null", txtStructId.Text);
            }
            else
            {
                lblMessage.Text = objService.UpdateTable("struct", "arabinan", txtarabinan.Text, txtStructId.Text);
            }

            if (txtmannan.Text == string.Empty)
            {
                lblMessage.Text = objService.UpdateTable("struct", "mannan", "null", txtStructId.Text);
            }
            else
            {
                lblMessage.Text = objService.UpdateTable("struct", "mannan", txtmannan.Text, txtStructId.Text);
            }

            clearTextBoxes();

            bindDataGrid();

        }
        else
        {

           

            lblMessage.Text = objService.SaveStruct(txtLignin.Text, txtLigninMethod.Text, txtCellulose.Text, txtHemiCellulose.Text, txtExtractives.Text, txtCellWall.Text, txtPectin.Text, txtSolubleSugarStarch.Text, txtXylan.Text, txtGlucomanna.Text, txtgalactan.Text, txtarabinan.Text, txtmannan.Text);

            string StructAnalID = objService.GetLastInsertedId("ref_fk", ReferenceID, "struct");

            objService.UpdateTable("struct", "ref_fk", ReferenceID , StructAnalID);

            clearTextBoxes();                      

            if ((string)Session["ExperimentId"] != null)
            {
                string ExpId = (string)Session["ExperimentId"];

                if (objService.UpdateExperiment("struct_fk", StructAnalID, ExpId) != "Experiment Successfully Updated")
                {
                    objService.DeleteRecord("struct", Convert.ToInt32(StructAnalID));
                    lblMessage.Text = "Admin - You can't enter the multiple Structural Analysis records for one experiment";
                    bindDataGrid();
                }
                else
                {
                    lblMessage.Text = "Structural Analysis Saved and " + objService.UpdateExperiment("struct_fk", StructAnalID, ExpId);
                    bindDataGrid();
                }

                lblExpdetails.Text = "Your Experiment Id is - " + ExpId;
            }
            else
            {

                lblMessage.Text = "Structural Analysis Saved and " + objService.SaveExperiment(null, null, null, null, null, null, null, null, StructAnalID, null, null, ReferenceID,null);
                string ExperimentId = objService.GetExperimentId("struct_fk", StructAnalID);
                Session["ExperimentId"] = ExperimentId;
                bindDataGrid();

                lblExpdetails.Text = "Your Experiment Id is - " + ExperimentId;

            }
        }

    }
    protected void dgStruct_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        dgStruct.PageIndex = e.NewPageIndex;
        bindDataGrid();
    }
    protected void btnPrevious_Click(object sender, EventArgs e)
    {
        Response.Redirect("Previous Pages/PreStruct.aspx");
    }
}
